<?php
/**
 * Created by PhpStorm.
 * Author: daisc
 * QQ: 2444329889
 * Time: 2018/02/06 10:00
 * Desc: desc
 */

class Controller_Mobile_News extends Stourweb_Controller
{
    private $_typeid = 115;   //产品类型
    private $_cache_key = '';
    function before()
    {
        parent::before();
        $this->_cache_key = Common::get_current_url();
        $html = Common::cache('get', $this->_cache_key);
        $genpage = intval(Arr::get($_GET, 'genpage'));
        if (!empty($html) && empty($genpage))
        {
            echo $html;
            exit;
        }

        $channelname = Model_Nav::get_channel_name_mobile($this->_typeid);
        $this->assign('typeid', $this->_typeid);
        $this->assign('channelname', $channelname);
        $this->assign('is_tpl', $this->request->action());

    }


    /**
     * @function 首页
     */
    public function action_index()
    {
        $seoinfo = Model_Nav::get_channel_seo_mobile($this->_typeid);
        $this->assign('seoinfo', $seoinfo);
        $this->display('../mobile/news/index', 'news_index');
        //缓存内容
        $content = $this->response->body();
        Common::cache('set', $this->_cache_key, $content);
    }


    /**
     * @function 列表
     */
    public function action_list()
    {
        $category_id = $this->request->param('categoryid');
        if(empty($category_id))
        {
            echo 'no categoryId';
            exit();
        }
        $category_info = Model_News_Attr::get_category_info($category_id);
        if($category_info['pid']!=0)
        {
            $parent_category_info = Model_News_Attr::get_category_info($category_info['pid']);
        }
        else
        {
            $parent_category_info = $category_info;
        }
        $p = intval($this->request->param('p'));

        $p = $p ? $p :1;
        $seotitle = $category_info['seotitle'] ? $category_info['seotitle'] : $category_info['attrname'];
        $seo_info = array(
            'seotitle' => $seotitle,
            'keyword' => $category_info['keyword'],
            'description' => $category_info['description']
        );
        $this->assign('page',$p);
        $this->assign('category_id',$category_id);
        $this->assign('category_info',$category_info);
        $this->assign('parent_category_info',$parent_category_info);
        $this->assign('seoinfo',$seo_info);
        $this->display('../mobile/news/list', 'news_list');
        //缓存内容
        $content = $this->response->body();
        Common::cache('set', $this->_cache_key, $content);


    }


    /**
     * @function 详情
     */
    public function action_show()
    {

        $aid = $this->request->param('aid');

        $info = Model_News::detail_aid($aid);

        if(empty($info))
        {
            Common::head_404();
        }


        //点击率增加
        Model_News::update_click_num($info['id']);

        $seoInfo = Product::seo($info);

        $this->assign('seoinfo', $seoInfo);
        $this->assign('info', $info);

        $show_templat = '../mobile/news/show';
        $this->display($show_templat, 'news_show','');
        //缓存内容
        $content = $this->response->body();
        Common::cache('set', $this->_cache_key, $content);
    }


    /**
     * @function 加载更多
     */
    public function action_ajax_get_more()
    {
        $page = intval($_POST['page']);
        $page = $page ? $page :1;
        $category = intval($_POST['category']);
        $pagesize = 10 ;
        if(!$category)
        {
            exit(json_encode(array('status'=>0)));
        }
        $offset = ($page-1) * $pagesize;
        $params = array(
            'flag'=>'category',
            'offset'=>$offset,
            'row'=>$pagesize,
            'categoryid'=>$category
        );
        $list = Taglib_News::query($params);


        foreach ($list as &$v)
        {
            $v['litpic'] = Common::img($v['litpic'], 220, 150);
            $v['addtime'] = date('Y-m-d',$v['addtime']);
        }
        unset($v);

        count($list)<$pagesize ?  $outpage = -1 :  $outpage = ++$page;

        exit(json_encode(array('status'=>1,'page'=>$outpage,'list'=>$list)));
    }







}